package com.kgc.controller;

import com.kgc.entity.User;
import com.kgc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/*@Controller
@RequestMapping("user")*/
public class UserController {

    @Autowired
    private UserService userService;


    //user/find?id=101 id 是请求参数
    @RequestMapping(value="find/{iid}/{name}",method = RequestMethod.POST)
    public String findById(@PathVariable(value = "iid") int id,
                           @PathVariable("name") String name){
        System.out.println("id="+id+",name="+name);
        return "list";
    }

    //这个方法接受客户端put请求
    @RequestMapping(value="update/{id}",method = RequestMethod.PUT)
    public String put(@PathVariable("id") int id,String name){
        System.out.println("update............."+id+","+name);
        return "list";
    }

    @RequestMapping(value="del/{id}",method = RequestMethod.DELETE)
    public String delete(@PathVariable("id") int id){
        System.out.println("delete............."+id);
        return "list";
    }

    @RequestMapping("save")
    public void saveUser3(User user, HttpServletResponse response, HttpServletRequest request, HttpSession session, Map map)throws Exception{
        userService.save(user);
        System.out.println(user);//user中id的
        response.sendRedirect("/user/list");

    }

    @RequestMapping("list")
    public ModelAndView list(){
        //封装转跳转的视图，充当作用对象
        ModelAndView mav=new ModelAndView();
        List<User> list=userService.findAll();
        mav.addObject("list",list);
        mav.addObject("msg","hello");
        mav.setViewName("list");
        return mav;
    }
    /*@RequestMapping("list")
    public ModelAndView list(){
        //封装转跳转的视图，充当作用对象
        ModelAndView mav=new ModelAndView();
        List<User> list=userService.findAll();
        mav.addObject("list",list);
        mav.addObject("msg","hello");
        mav.setViewName("list");
        return mav;
    }
    public String list1(Map model){
         List<User> list=userService.findAll();
         model.put("list",list);
         return "list";
    }

    //doGet
    //使用一个对象来接受表单中值  约定  表单name要与属性setXXXX相同
    @RequestMapping("save")
    public ModelAndView saveUser4(User user)throws Exception{
        userService.save(user);
        System.out.println(user);//user中id的
        return null;
    }
    public void saveUser3(User user, HttpServletResponse response, HttpServletRequest request, HttpSession session, Map map)throws Exception{
        userService.save(user);
        System.out.println(user);//user中id的
        response.sendRedirect("/user/list");

    }

    public String saveUser2(User user){
        userService.save(user);
        System.out.println(user);//user中id的
        return "redirect:/user/list";
    }
    public String saveUser1(@RequestParam(value = "userName") String name,
                           String pwd,
                           @RequestParam(value = "age",required = false,defaultValue = "0") int age, String addr){
        System.out.println(name+","+pwd+','+age+","+addr);
        return  "list";
    }*/
}
